package com.prs.crm.dao.sys.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sys.AccountSetDao;
import com.prs.crm.domain.ca.work.ArticleTook;
import com.prs.crm.domain.sys.AccountSet;
import com.prs.crm.domain.sys.SysDepartment;

public class AccountSetDaoImpl extends BaseDaoImpl<AccountSet> implements AccountSetDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountSet> getAllAccountSet() {
		Criteria criteria=this.getSession().createCriteria(AccountSet.class);
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<AccountSet> findBySysDepartment(
			final SysDepartment sysDepartment) {
		Criteria criteria=this.getSession().createCriteria(AccountSet.class);
		criteria.add(Restrictions.eq("sysDepartment", sysDepartment));
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public AccountSet getAccountSetByUserId(final String userId) {
		return (AccountSet) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(AccountSet.class);
				criteria.add(Restrictions.eq("userId", userId));
				List<AccountSet> accountSets = criteria.list();
				if (accountSets.size() == 0) {
					return null;
				} else {
					return accountSets.get(0);
				}
			}
		});
	}

}
